﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class TransactionDataGridAdapter
    {
        InstructionDataDTO _dto;
        InstructionUnitTrustGridDetail _unitTrustDTO;
        InstructionEquityGridDetail _equityDTO;
        InstructionDepositAtFinancialGridDetail _depositDTO;

        public string UnitTrustTransactionType
        {
            get { return _unitTrustDTO.UnitTrustTransactionType; }
        }
        public string UnitTrustCounterPartyName
        {
            get { return _unitTrustDTO.UnitTrustCounterPartyName; }
        }
        public decimal? UnitTrustTimeToMaturity
        {
            get { return _unitTrustDTO.UnitTrustTimeToMaturity; }
        }
        public DateTime? UnitTrustTradeDate
        {
            get { return _unitTrustDTO.UnitTrustTradeDate; }
        }
        public DateTime? UnitTrustSettlementDate
        {
            get { return _unitTrustDTO.UnitTrustSettlementDate; }
        }
        public int? UnitTrustUnit
        {
            get { return _unitTrustDTO.UnitTrustUnit; }
        }
        public int? UnitTrustRemainUnit
        {
            get { return _unitTrustDTO.UnitTrustRemainUnit; }
        }
        public decimal? UnitTrustPar
        {
            get { return _unitTrustDTO.UnitTrustPar; }
        }
        public decimal? UnitTrustNav
        {
            get { return _unitTrustDTO.UnitTrustNav; }
        }
        public DateTime? UnitTrustNavDate
        {
            get { return _unitTrustDTO.UnitTrustNavDate; }
        }
        public decimal? UnitTrustBidPrice
        {
            get { return _unitTrustDTO.UnitTrustBidPrice; }
        }
        public DateTime? UnitTrustBidPriceDate
        {
            get { return _unitTrustDTO.UnitTrustBidPriceDate; }
        }

        public decimal? UnitTrustPurchaseValueNet
        {
            get { return _unitTrustDTO.UnitTrustPurchaseValueNet; }
        }

        public decimal? UnitTrustBidPriceAmt
        {
            get { return _unitTrustDTO.UnitTrustBidPriceAmt; }
        }







        public string EquityTransactionType
        {
            get { return _equityDTO.EquityTransactionType; }
        }
        public string EquityCounterPartyName
        {
            get { return _equityDTO.EquityCounterPartyName; }
        }
        public decimal? EquityTimeToMaturity
        {
            get { return _equityDTO.EquityTimeToMaturity; }
        }
        public DateTime? EquityTradeDate
        {
            get { return _equityDTO.EquityTradeDate; }
        }
        public DateTime? EquitySettlementDate
        {
            get { return _equityDTO.EquitySettlementDate; }
        }
        public int? EquityUnit
        {
            get { return _equityDTO.EquityUnit; }
        }
        public int? EquityRemainUnit
        {
            get { return _equityDTO.EquityRemainUnit; }
        }
        public decimal? EquityPar
        {
            get { return _equityDTO.EquityPar; }
        }
        public decimal? EquityNav
        {
            get { return _equityDTO.EquityNav; }
        }
        public DateTime? EquityNavDate
        {
            get { return _equityDTO.EquityNavDate; }
        }
        public decimal? EquityBidPrice
        {
            get { return _equityDTO.EquityBidPrice; }
        }
        public DateTime? EquityBidPriceDate
        {
            get { return _equityDTO.EquityBidPriceDate; }
        }

        public decimal? EquityPurchaseValueNet
        {
            get { return _equityDTO.EquityPurchaseValueNet; }
        }

        public decimal? EquityBidPriceAmt
        {
            get { return _equityDTO.EquityBidPriceAmt; }
        }


        public string DepositVoucherNo { get { return _depositDTO.VoucherNo; } }

        public decimal? DepositAmount { get { return _depositDTO.Amount; } }
        public string DepositTerm { get { return _depositDTO.Term; } }
        public string DepositTransactionType { get { return _depositDTO.TransactionType; } }
        public string DepositBankName { get { return _depositDTO.BankName; } }
        public string DepositBankBranchName { get { return _depositDTO.BankBranchName; } }
        public decimal? DepositTtm { get { return _depositDTO.Ttm; } }
        public DateTime? DepositSettlementDate { get { return _depositDTO.SettlementDate; } }




        public TransactionDataGridAdapter(InstructionDepositAtFinancialGridDetail dto)
        {
            _depositDTO = dto;
        }

        public TransactionDataGridAdapter(InstructionUnitTrustGridDetail dto)
        {
            _unitTrustDTO = dto;
        }

        public TransactionDataGridAdapter(InstructionEquityGridDetail dto)
        {
            _equityDTO = dto;
        }

        public TransactionDataGridAdapter(InstructionDataDTO dto)
        {
            _dto = dto;
            TransactionType = dto.TransactionType;


            TransactionType = dto.TransactionType;
            SBI_REPO_Type = dto.SBI_REPO_Type;
            CounterParty = dto.CounterParty;


            TimeToMaturityDate = WebUtil.NonApplicableIfZero(dto.TimeToMaturityDate);
            Transaction = dto.Transaction;
            TradeDate = dto.TradeDate;
            SettlementDate = dto.SettlementDate;
            TransactionUnit = dto.TransactionUnit;
            RemainUnit = dto.RemainUnit;
            Par = dto.Par.GetValueOrDefault();
            CostYield_YTM = dto.CostYieldYTM.GetValueOrDefault();
            CouponPaymentDate = dto.CouponPaymentDate;
            Coupon = dto.Coupon.GetValueOrDefault();
            PurchaseValue_LocalTHB = dto.PurchaseValueLocalTHB;

            PurchaseID = dto.PurchaseID;
            FixincomeHeaderID = dto.FixincomeHeaderID;
            InstructionDetailID = dto.InstructionDetailID;
            TransactionID = dto.TransactionID;
            IsinCode = dto.IsinCode;
            Symbol = dto.Symbol;
            MaturityDate = dto.MaturityDate;
            RefNo = dto.RefNo;
            TypeOfPaymentName = dto.TypeOfPaymentName;
        }

        public bool Selected { set; get; }
        public String TransactionType { set; get; }
        public String SBI_REPO_Type { set; get; }
        public String CounterParty { set; get; }
        public String TimeToMaturityDate { set; get; }
        public String Transaction { set; get; }
        //   [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? TradeDate { set; get; }
        //  [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? SettlementDate { set; get; }
        public int TransactionUnit { set; get; }
        public int RemainUnit { set; get; }
        public decimal Par { set; get; }
        public decimal CostYield_YTM { set; get; }
        //   [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? CouponPaymentDate { set; get; }
        public decimal Coupon { set; get; }
        public decimal? PurchaseValue_LocalTHB { set; get; }
        public String IsinCode { set; get; }
        public String Symbol { set; get; }
        public int PurchaseID { set; get; }
        public int FixincomeHeaderID { set; get; }
        public int InstructionDetailID { set; get; }
        public DateTime? MaturityDate { set; get; }
        public String RefNo { set; get; }

        /// <summary>
        /// null ถ้ายังไม่มีการขาย
        /// </summary>
        public int? TransactionID { set; get; }

        public int Key { set; get; }


        public InstructionDetail ToInstructionDetailFixIncome()
        {
            InstructionDetail detail = new InstructionDetail();
            detail.InstructionCounterpartyID = _dto.CounterPartyID;
            detail.InstructionCounterpartyName = _dto.CounterParty;
            detail.InstructionDetailID = _dto.InstructionDetailID;

            detail.InstructionNOCoupon = _dto.Coupon;
            detail.InstructionNOCouponPaymentDate = _dto.CouponPaymentDate;
            detail.InstructionNOInstructionCounterpartyID = _dto.CounterPartyID;
            detail.InstructionNOInstructionCounterpartyName = _dto.CounterParty;
            detail.InstructionNOIssuerTerm = _dto.IssuerTerm;
            detail.InstructionNOPar = _dto.Par;
            detail.InstructionNOPurchaseValue = _dto.PurchaseValueLocalTHB;
            detail.InstructionNOSettlementDate = _dto.SettlementDate;
            detail.InstructionNOTradeDate = _dto.TradeDate;
            detail.InstructionNOTransactionType = _dto.TransactionType;
            detail.InstructionNOUnit = _dto.TransactionUnit;
            detail.InstructionNOYield = _dto.CostYieldYTM;
            detail.InstructionRefNO = _dto.RefNo;
            detail.InstrumentHeaderID = _dto.FixincomeHeaderID;
            detail.InstrumentPurchaseID = _dto.PurchaseID;
            detail.InstrumentTransactionID = _dto.TransactionID;
            detail.MaturityDate = _dto.MaturityDate;
            // detail.ty
            return detail;
        }

        public string TypeOfPaymentName { get; set; }

        public UnitTrustInstructionDetail ToInstructionDetailUnitTrust()
        {

            UnitTrustInstructionDetail detail = new UnitTrustInstructionDetail();
            detail.InstrumentHeaderID = _unitTrustDTO.UnitTrustHeaderID;
            detail.InstrumentTransactionID = _unitTrustDTO.UnitTrustTransactionID;
            detail.HeaderID = _unitTrustDTO.UnitTrustInstructionHeaderID;
            detail.DetailID = _unitTrustDTO.UnitTrustInstructionDetailID.GetValueOrDefault();
            detail.TransactionType = _unitTrustDTO.UnitTrustTransactionType;
            detail.FundName = _unitTrustDTO.UnitTrustFundName;
            detail.CounterpartyName = _unitTrustDTO.UnitTrustCounterPartyName;
            detail.Unit = _unitTrustDTO.UnitTrustUnit;
            detail.PriceUnit = _unitTrustDTO.UnitTrustPriceUnit;
            detail.GrossAmount = _unitTrustDTO.UnitTrustGrossAmt;
            detail.ComAmount = _unitTrustDTO.UnitTrustComAmt;
            detail.VatAmount = _unitTrustDTO.UnitTrustVatAmt;
            detail.PurchaseValueNet = _unitTrustDTO.UnitTrustPurchaseValueNet;
            detail.WHTaxAmount = _unitTrustDTO.UnitTrustWhTaxAmt;
            detail.NetSettle = _unitTrustDTO.UnitTrustNetSettle;
            detail.TypeofpaymentName = _unitTrustDTO.UnitTrustPaymentType;
            detail.RefNO = _unitTrustDTO.UnitTrustRefNo;
            detail.ListedStatus = _unitTrustDTO.UnitTrustListStatus;
            return detail;
        }
        public DepositInstructionDetail ToDepositInstructionDetail()
        {

            DepositInstructionDetail detail = new DepositInstructionDetail();


            detail.InstructionHeaderID = _depositDTO.HeaderID;
            detail.DepositInstructionDetailID = _depositDTO.DetailID.GetValueOrDefault();
            detail.InstrumentHeaderID = _depositDTO.InstrumentHeaderID;
            detail.InstrumentTransactionID = _depositDTO.InstrumentTransactionID;
            detail.InstructionTransactionType = _depositDTO.TransactionType;
            detail.InstructionAmount = _depositDTO.Amount;
            detail.InstructionBranchName = _depositDTO.BankBranchName;
            detail.InstructionBankName = _depositDTO.BankName;
            detail.InstructionInterestRate = _depositDTO.InterestRate;
            detail.InstructionMaturityDate = _depositDTO.MaturityDate;
            detail.InstructionRefNO = _depositDTO.RefNo;
            detail.InstructionSettlementDate = _depositDTO.SettlementDate;
            detail.InstructionTradeDate = _depositDTO.TradeDate;
            //detail .= _depositDTO.Ttm;
            detail.InstructionTypeofpayment = _depositDTO.TypeOfPayment;
            detail.InstructionAccountVoucerNO = _depositDTO.VoucherNo;
            detail.InstructionTerm = _depositDTO.Term;




            //   detail.ListedStatus = _equityDTO.EquityListStatus;
            return detail;
        }
        public EquityInstructionDetail ToEquityInstructionDetail()
        {

            EquityInstructionDetail detail = new EquityInstructionDetail();
            detail.InstrumentHeaderID = _equityDTO.EquityHeaderID;
            detail.InstrumentTransactionID = _equityDTO.EquityTransactionID;
            detail.HeaderID = _equityDTO.EquityInstructionHeaderID;
            detail.DetailID = _equityDTO.EquityInstructionDetailID.GetValueOrDefault();
            detail.TransactionType = _equityDTO.EquityTransactionType;
            detail.SecName = _equityDTO.EquitySecName;
            detail.CounterpartyName = _equityDTO.EquityCounterPartyName;
            detail.Unit = _equityDTO.EquityUnit;
            detail.PriceUnit = _equityDTO.EquityPriceUnit;
            detail.GrossAmount = _equityDTO.EquityGrossAmt;
            detail.ComAmount = _equityDTO.EquityComAmt;
            detail.VatAmount = _equityDTO.EquityVatAmt;
            detail.PurchaseValueNet = _equityDTO.EquityPurchaseValueNet;
            detail.WHTaxAmount = _equityDTO.EquityWhTaxAmt;
            detail.NetSettle = _equityDTO.EquityNetSettle;
            detail.TypeofpaymentName = _equityDTO.EquityPaymentType;
            detail.RefNO = _equityDTO.EquityRefNo;
            //   detail.ListedStatus = _equityDTO.EquityListStatus;
            return detail;
        }
    }
}